<?php

namespace Rblog\Module\Index\Model;

class Art extends \Rexzy\App\Model {
    
    protected $_table = 'arts';
    
    /**
     * 查询一组文章里每篇文章的分类信息
     * 
     * @param array() $arts 需要查询的文章
     * @return array()
     */
    public function artSortMesg( $arts ) {
        $sortDate = \Rexzy::model( 'Sort' );
        foreach ( $arts as $key => $val ) {
            $sorts = explode( ',', $val['sort'] );
            $arts[$key]['sort'] = $sorts;
            $arts[$key]['sortName'] = array();
            foreach ( $sorts as $key1 => $val1 ) {
                $sortName = $sortDate->select( 'name, slugname', array( 'sortid' => $val1 ) );
                $arts[$key]['sortName'][$key1] = $sortName['name']; 
                $arts[$key]['sortSlugName'][$key1] = $sortName['slugname']; 
            }
        }
        return $arts;
    }
    
    
    /**
     * 查询博客前台主页显示的文章信息
     * 
     * @return array 查询结果
     */
    public function index() {
        $where = "group by artid order by artid desc";
        $limit = 10;
        $result = $this->selectAll( '*', $where, $limit );
        foreach ( $result as $key => $val ) {
            $result[$key]['date'] = date( 'Y-m-d', $val['date'] );
            $result[$key]['outline'] = nl2br( $val['outline'] );
        }
        $arts = $this->artSortMesg( $result );
        
        return $arts;
    }
    
    /**
     * 查询属于某个分类的文章
     * 
     * @param int $sortid 查询分类的id
     * @return array() 查询结果
     */
    public function sortArts( $sortids ) {
        $arts = array();
        foreach( $sortids as $key => $val ) {
            $where = "where find_in_set(?, sort) group by artid order by artid desc";
            $params = array( $val );
            $result = $this->selectAll( '*', $where, '', $params );
            foreach ( $result as $key => $val ) {
                $exist = false;
                foreach( $arts as $key1 => $val1 ) {
                    if ( $result[$key]['artid'] == $val1['artid'] ) {
                        $exist = true;
                    }
                }
                if ( $exist != true ) {
                    $result[$key]['date'] = date( 'Y-m-d', $val['date'] );
                    $arts[] = $result[$key];
                }
            }
        }
        $arts = $this->artSortMesg( $arts );
        
        if ( count( $arts ) > 0 ) {
            foreach( $arts as $key => $val ) {
                $artids[$key] = $val['artid']; 
            }

            array_multisort( $artids, SORT_DESC, $arts );
        }
        
        return $arts;
        
    }
    
    /**
     * 查询某篇文章的信息内容
     * 
     * @param string $titleName 查询的文章标题
     * @return array() 查询结果
     */
    public function getArtMesg( $titleName ) {
        $result = $this->select( '*', $titleName );
        if ( count( $result ) != 0 ) {
            $result['date'] = date( 'Y-m-d', $result['date'] );
            return $result;
        } else {
            return false;
        }
    }
    
    /**
     * 搜索包含关键字的标题的文章
     * 
     * @param  string $keyword 搜索关键字
     * @return array()
     */
    public function searchTitle( $keyword ) {
        $where = "where title like ? group by artid order by artid desc";
        $params = array( "%" . $keyword . "%" );
        $result = $this->selectAll( '*', $where, '', $params );
        foreach ( $result as $key => $val ) {
            $result[$key]['date'] = date( 'Y-m-d', $val['date'] );
        }
        
        $arts = $this->artSortMesg( $result );
        return $arts;
    }
    
    /**
     * 查询某个作者的所有文章
     * 
     * @param string $author 作者名
     * @return array()
     */
    public function author( $author ) {
        $where = "where author = ? group by artid order by artid desc";
        $params = array( $author );
        $result = $this->selectAll( '*', $where, '', $params );
        foreach ( $result as $key => $val ) {
            $result[$key]['date'] = date( 'Y-m-d', $val['date'] );
        }
        
        $arts = $this->artSortMesg( $result );
        return $arts;
    }
    
    /**
     * 查询某个日期前的所有文章
     * 
     * @param date $date 搜索的时间戳
     * @return array()
     */
    public function date( $date ) {
        $where = "where date <= ? group by artid order by artid desc";
        $params = array( $date );
        $result = $this->selectAll( '*', $where, '', $params );
        foreach ( $result as $key => $val ) {
            $result[$key]['date'] = date( 'Y-m-d', $val['date'] );
        }
        
        $arts = $this->artSortMesg( $result );
        return $arts;
    }
}
